<?php
// 应用公共文件

function successJson($data = [], $message = '请求成功', $code = 200) {
    return json([
        'code' => $code,
        'message' => $message,
        'data' => $data,
    ]);
}

function errorJson($message = '请求失败', $code = 0) {
    return json([
        'code' => $code,
        'message' => $message,
        'data' => [],
    ]);
}

function throwError($msg = '请求失败', $code = 0) {
    throw new Exception($msg, $code);
}

function generateSign($length = 8) {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*';
    $str = '';
    for ($i = 0; $i < $length; $i++) {
        $str .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $str;
}